package research.ui.editors;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import research.domain.Component;
import research.domain.ComponentType;
import research.entity.EntityType;
import research.model.EntityListContentProvider;
import research.model.ListHierarchy;
import research.persistence.PersistenceManager;
import research.ui.editors.input.EntityEditorInput;

public class ComponentEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.ComponentEditor";
	private Text name;
	private Text description;
	private ComboViewer componentType;
	
	private Component getEntity() {
		return (Component) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.Component);
	}

	@Override
	protected void fill() {
		this.name.setText(this.getEntity().getName());
		this.description.setText(this.getEntity().getDescription());
		
		setComboEntity(componentType, this.getEntity().getComponentType());
	}

	@Override
	protected void initialize() {
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setName(this.name.getText());
		this.getEntity().setDescription(this.description.getText());
		
		ComponentType cal = (ComponentType) getComboEntity(componentType);
		
		if (cal == null) {
			MessageDialog.openWarning(getSite().getShell(), "Error",
					"Выберите тип компонента");
			return false;
		}
		
		this.getEntity().setComponentType(cal);

		return validateText(name);
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Section section;
		Composite composite;
		GridData gd;
		Label label;
		
		/*
		 * Name
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Название*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		name = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		name.setLayoutData(gd);
		name.addModifyListener(getNameModifyListener());
		
		/*
		 * Component Type
		 */		
		gd = new GridData();
		label = toolkit.createLabel(composite, "Тип*:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		componentType = new ComboViewer(composite, SWT.DROP_DOWN
				| SWT.READ_ONLY);
		componentType.setContentProvider(new EntityListContentProvider(new ListHierarchy(EntityType.ComponentType), false));
		componentType.setLabelProvider(new LabelProvider());
		componentType.setInput(PersistenceManager.getInstance());
		gd.widthHint = 100;
		componentType.getCombo().setLayoutData(gd);
		componentType.getCombo().addModifyListener(getTextModifyListener());
		
		/*
		 * Description
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Описание");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		description = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		description.setLayoutData(gd);
		description.addModifyListener(getTextModifyListener());

	}

}
